#!/usr/bin/python
# -*- coding: utf-8 -*-


# Qt Libs
from PyQt4.QtCore import *
from PyQt4.QtGui import *

# KDE Libs
from PyKDE4.kdecore import *
from PyKDE4.kdeui import *

# Plasma Libs
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript

import hashlib, libkontrolcu

class PmDragApplet(plasmascript.Applet):

    def __init__(self, parent, args=None):
        plasmascript.Applet.__init__(self, parent)

    def init(self):
        self.setHasConfigurationInterface(False)
        self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
        self.theme = Plasma.Svg(self)
        self.theme.setImagePath("widgets/background")
        self.theme.resize(self.size())
        self.applet.setAcceptDrops(True)
        self.layout = QGraphicsLinearLayout(self.applet)
        self.layout.setContentsMargins(0,0,0,0)
        self.layout.setSpacing(0)
        self.layout.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding))
        self.icon = Plasma.IconWidget()
        self.icon.setIcon("configure")
        self.icon.setToolTip("Üzerine bir dosya sürükleyip, bırakın.")
        self.icon.setAcceptDrops(False)
        self.layout.addItem(self.icon)
	self.layout.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding))
        self.label = Plasma.Label(self.applet)
        self.label.setText(u"Kontrolcu Hazır!")
        self.label.setAlignment(Qt.AlignCenter)
        self.layout.addItem(self.label)
	self.connect(self.icon, SIGNAL("clicked()"), self.sendClipboard)
    def constraintsEvent(self, constraints):
        if constraints & Plasma.SizeConstraint:
            self.theme.resize(self.size())
    def sendClipboard(self):
        a = self.label.text()
        if str(a) == "Kontrol kopyalandı.":
            print ""
	elif str(a) == "Kontrolcu Hazır!":
	    print ""
        else:
	    liste = a.split(" ")
	    a = liste[-1]
	    clipboard = QApplication.clipboard()
	    clipboard.setText(a) 
	    self.label.setText(u"Kontrol kopyalandı.")

    def dragEnterEvent(self, event):
        event.acceptProposedAction()

    def dragMoveEvent(self, event):
        event.accept()

    def dropEvent(self, event):
	if str(event.mimeData().text()):
	    dosyagir = event.mimeData().text()
            dosyagir = dosyagir[7:]
	    toplam = libkontrolcu.toplamver(hashlib.md5,dosyagir)
	    cikti = "Çıktısı: %s" % toplam
	    self.label.setText(unicode(cikti))
    def mousePressEvent(self, event):
        pass

    
def CreateApplet(parent):
    return PmDragApplet(parent)

